home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Games of Daze
/
Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso
/
x2ftp
/
msdos
/
docs
/
winer
/
evaluate.bas
< prev
next >
Wrap
BASIC Source File
|
1992-05-13
|
1KB
|
45 lines
'********** EVALUATE.BAS - simple expression evaluator
'Copyright (c) 1992 Ethan Winer
DECLARE FUNCTION Evaluate# (Formula$)
INPUT "Enter an expression: ", Expr$
PRINT "That evaluates to"; Evaluate#(Expr$)
FUNCTION Evaluate# (Formula$)
'Search for an operator using INSTR as a
'table lookup. If found, remember which
'one and also its position in the string.
FOR Position% = 1 TO LEN(Formula$)
Operation% = INSTR("+-*/", MID$(Formula$, Position%, 1))
IF Operation% THEN EXIT FOR
NEXT
'Get the value of the left part, and a
'tentative value for the right portion.
LeftVal# = VAL(Formula$)
RightVal# = VAL(MID$(Formula$, Position% + 1))
'See if there's another level to evaluate.
Paren% = INSTR(Position%, Formula$, "(")
'If there is, call ourself for a new RightVal#.
IF Paren% THEN RightVal# = Evaluate#(MID$(Formula$, Paren% + 1))
'No more to evaluate. Perform the appropriate
'operation and exit.
SELECT CASE Operation%
CASE 1
Evaluate# = LeftVal# + RightVal#
CASE 2
Evaluate# = LeftVal# - RightVal#
CASE 3
Evaluate# = LeftVal# * RightVal#
CASE 4
Evaluate# = LeftVal# / RightVal#
END SELECT
END FUNCTION